package com.example.springcloudorderserver1030.controller;

import com.example.springcloudusercommon.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 温伟雄
 * @Date: 2021/10/12/15:04
 * @Description:
 */
@RestController
public class OrderController {
    //需要配置成Bean
    @Autowired
    private RestTemplate restTemplate ;

    //浏览器调用该方法
    @HystrixCommand(fallbackMethod = "fallbackMethod") //方法熔断
    @RequestMapping(value = "/order/{id}",method = RequestMethod.GET)
    public User getById(@PathVariable("id")Long id){
        //发送http请求调用 user的服务，获取user对象 ： RestTemplate
        //目标资源路径：user的ip,user的端口，user的Controller路径
        String url = "http://user-server/user/"+id;

        //发送http请求
        return restTemplate.getForObject(url, User.class);
    }

    //降级方法 ， 参数和返回值必须和被熔断的方法一致 ，方法名要和  fallbackMethod 的值一致
    public User fallbackMethod(@PathVariable("id")Long id){
        //返回托底数据
        return new User(-1L ,"无此用户","用户服务不可用");
    }


}

